package com.ftp.web.spider.utils;

import java.io.File;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import com.ftp.web.spider.entity.FileInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SearchFile {

    private Logger logger= LoggerFactory.getLogger(SearchFile.class);

    /*public static void main(String[] args) {
        File path=new File("D:\\Config");
        List<String[]> md5list=new ArrayList<>();
        List<String[]> list=fileSes(md5list,path);
        for(String[] s:list){
            System.out.println("name:"+s[0]+"\tmd5:"+s[1]+"\tpath:"+s[2]+"\tsize:"+s[3]);
        }
    }*/

    public  List<FileInfo> fileSees(List<FileInfo> list , File file){

        try {
            InetAddress inetAddress=InetAddress.getLocalHost();
            String machineName=inetAddress.getHostName();
            String machineIp=inetAddress.getHostAddress();
            System.out.println(file.getName());
            if(file.canRead()){
                File[] file1=file.listFiles();
                for(File file2:file1){
                    if(file2.isDirectory()){
                        //System.out.println(file2);
                        fileSees(list,file2);
                        logger.info("扫描路径--{}--下的文件",file2.getName());
                    }else {
                        String md5=CountFileMd5.count(file2.getPath());
                        //注入 文件 名称,路径,大小,后缀,所在机器名,存在机器ip
                        FileInfo fileInfo=new FileInfo();
                        fileInfo.setFileName(file2.getName());
                        fileInfo.setFilePath(file2.getPath());
                        fileInfo.setFileSize(file2.length()/1024);
                        fileInfo.setFileSuffix(FilenameUtils.getExtension(file2.getName()).toLowerCase());
                        fileInfo.setMachine(machineName);
                        fileInfo.setMachineIp(machineIp);
                        fileInfo.setMd5(md5);
                        list.add(fileInfo);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
